#coding=utf-8
'''
Created on 2017-6-15

@author: sandy
'''

from twisted.internet.protocol import ReconnectingClientFactory
from twisted.internet import reactor
import logging
from twisted.internet import protocol

class ProxyFactory(ReconnectingClientFactory):
    def __init__(self):
        ReconnectingClientFactory.maxDelay = 5
    
    def buildProtocol(self, addr):
        logging.warn(u"Proxy CONNECTED!!!")
        self.resetDelay()
        return instance
    
    def startedConnecting(self, connector):
        logging.warn(u"Proxy connecting!!!")
    
    def clientConnectionLost(self, connector, reason):
        logging.warn(u"Proxy DOWN!!!")
        ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
        
    def clientConnectionFailed(self, connector, reason):
        logging.warn(u"Proxy connect FAIL!!!")
        ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)

class Proxy(protocol.Protocol):
                      
    def __init__(self):
        pass
    
    def start(self): 
        reactor.connectTCP("127.0.0.1",8900,ProxyFactory())
        
    def stop(self):
        pass
    
    def connectionMade(self):
        logging.warn(u"Proxy connectionMade()")

    def connectionLost(self, reason=protocol.connectionDone):
        logging.debug(u"Proxy connectionLost")
        
    def dataReceived(self, data):
        logging.debug(u"Proxy dataReceived")
    
instance = Proxy()
if __name__ == '__main__':
    pass